package com.pacifictextiles.qc.inspection.server.controller.servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import com.pacifictextiles.qc.inspection.server.model.dao.impl.RollDAOImpl;

/**
 * Servlet implementation class NewRollSVL
 */
public class NewRollSVL extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public NewRollSVL() {
        super();

    }

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
	
		response.setContentType("text/plain");
		response.setCharacterEncoding("UTF-8");

		// 获取请求参数
		String fullCode = request.getParameter("fullCode");
		String stationId = request.getParameter("stationId");
		String operatorId = request.getParameter("operatorId");

		// 数据库操作对象
		RollDAOImpl rdi = new RollDAOImpl();

		// 数据存储对象
		String[] rollNoAndIndex = new String[2];

		// JSON打包对象
		JSONPackager jp = new JSONPackager();
		
		PrintWriter out = response.getWriter();

		// 执行数据库操作
		try {
			// 插入新roll，返回rollNo和Index
			rollNoAndIndex = rdi.addNewRoll(fullCode, stationId, operatorId);

			if (rollNoAndIndex != null) {
				JSONObject rollNoAndIndexJSONObject = jp
						.packageRollNoAndIndex(rollNoAndIndex);
				
				out.write(rollNoAndIndexJSONObject.toString());
				
				out.flush();
			}

		} catch (Exception e) {
			// e.printStackTrace();
			System.out.print("New Roll Servlet:  打包roll对象失败" + "\n\n");
		}
		out.close();
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		doGet(request, response);
	}

}
